package com.example.mvitest.mvi

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.mvitest.R
import com.example.mvitest.databinding.AdapterItemBinding

class MyAdapter(list: List<String>) : RecyclerView.Adapter<MyAdapter.MyViewHolder>(){

    private val itemList = list
    private var onItemClick: ((Int) -> Unit)? = null

    inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
        var tvItem:TextView = itemView.findViewById(R.id.tv_item)
    }

    override fun getItemCount() = itemList.size


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.adapter_item,parent,false)
        return MyViewHolder(view)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.tvItem.text = itemList[position]
        holder.itemView.setOnClickListener {
            onItemClick?.invoke(position)
        }
    }

    fun setOnItemClick(onItemClick:(Int)->Unit){
        this.onItemClick = onItemClick
    }

}